Imports RIBS.BusinessLogicLayer.Globals
Imports RIBS.DataAccessLayer

Namespace BusinessLogicLayer

    Public Class SummaryReport
        Public Shared Function SummaryReport(ByVal ds As DataSet, Optional ByVal UserBased As Boolean = True) As DataSet
            Try
                Dim DBLayer As New DataAccess
                Dim tblOptions As DataTable = ds.Tables("Options")
                Dim tblUser As DataTable = ds.Tables("Users")
                Dim tblAction As DataTable = ds.Tables("Action")
                Dim rtds As New dsDBSchema
                Dim FromDate As DateTime
                Dim UserID As String = ""

                FromDate = tblOptions.Rows(0).Item("FromDate")
                Dim drOptions As DataRow
                drOptions = rtds.Tables("Options").NewRow
                drOptions("FromDate") = FromDate
                drOptions("ToDate") = tblOptions.Rows(0).Item("ToDate")
                drOptions("ReportTitle") = tblOptions.Rows(0).Item("ReportTitle")
                drOptions("PageTitle") = tblOptions.Rows(0).Item("PageTitle")
                drOptions("PageTitle") = tblOptions.Rows(0).Item("PageTitle")


                rtds.Tables("Options").Rows.Add(drOptions)

                Dim dtContactMethods As DataTable = DBLayer.GetContactMethods
                Dim dtAllocated As DataTable = DBLayer.GetDailySummaryReport(FromDate, Globals.RIBSState.Allocated.ToString)
                Dim dtPending As DataTable = DBLayer.GetDailySummaryReport(FromDate, Globals.RIBSState.Pending.ToString)
                Dim dtClosed As DataTable = DBLayer.GetDailySummaryReport(FromDate, Globals.RIBSState.Closed.ToString)
                Dim drSummart As DataRow = rtds.DailySummaryReportSummary.NewRow
                rtds.DailySummaryReportSummary.Rows.Add(drSummart)

                For Each dr As DataRow In dtAllocated.Rows
                    AddUserToTable(rtds.DailySummaryReportBody, dr("UserID").ToString, dtContactMethods)
                    Dim ldr As DataRow() = rtds.DailySummaryReportBody.Select("UserID ='" & dr("UserID").ToString & "'" & _
                        " AND ContactMethod = '" & ReportContactMethodDisplayName(dr("ContactMethod").ToString).ToString & "'")
                    Try
                        If ldr.Length > 0 Then
                            ldr(0).Item("Column1" & dr("Letter_Code").ToString) = dr("ContactMethodCount")
                            ldr(0).Item("Column1Dollars" & dr("Letter_Code").ToString) = dr("CURRENT_PREMIUM_AMOUNT")
                            ldr(0).Item("Column1" & dr("Letter_Code").ToString) = dr("RibsCount")
                            rtds.DailySummaryReportSummary.Rows(0)("UnTouched") += dr("ContactMethodCount")
                            rtds.DailySummaryReportSummary.Rows(0)("UnTouchedDollars") += dr("CURRENT_PREMIUM_AMOUNT")
                        End If
                    Catch ex As Exception
                        ldr(0).Item("DataError") += "Error: " & ex.Message & vbCrLf
                    End Try
                    
                Next
                For Each dr As DataRow In dtPending.Rows                    
                    AddUserToTable(rtds.DailySummaryReportBody, dr("UserID").ToString, dtContactMethods)
                    Dim ldr As DataRow() = rtds.DailySummaryReportBody.Select("UserID ='" & dr("UserID").ToString & "'" & _
                        " AND ContactMethod = '" & ReportContactMethodDisplayName(dr("ContactMethod").ToString).ToString & "'")
                    Try
                        If ldr.Length > 0 Then
                            ldr(0).Item("Column2" & dr("Letter_Code").ToString) = dr("ContactMethodCount")
                            ldr(0).Item("Column2Dollars" & dr("Letter_Code").ToString) = dr("CURRENT_PREMIUM_AMOUNT")
                            ldr(0).Item("Column2" & dr("Letter_Code").ToString) = dr("RibsCount")
                            rtds.DailySummaryReportSummary.Rows(0)("Pending") += dr("ContactMethodCount")
                            rtds.DailySummaryReportSummary.Rows(0)("PendingDollars") += dr("CURRENT_PREMIUM_AMOUNT")
                        End If
                    Catch ex As Exception
                        ldr(0).Item("DataError") += "Error: " & ex.Message & vbCrLf
                    End Try
                    
                Next
                For Each dr As DataRow In dtClosed.Rows
                    AddUserToTable(rtds.DailySummaryReportBody, dr("UserID").ToString, dtContactMethods)
                    Dim ldr As DataRow() = rtds.DailySummaryReportBody.Select("UserID ='" & dr("UserID").ToString & "'" & _
                                                " AND ContactMethod = '" & ReportContactMethodDisplayName(dr("ContactMethod").ToString).ToString & "'")
                    Try                        
                        If ldr.Length > 0 Then
                            Dim xAction As String = Replace(dr("xAction"), " ", "")
                            ldr(0).Item("Column3" & xAction) += NZint(dr("ContactMethodCount"), 0)
                            ldr(0).Item("Column3Dollars" & xAction) += NZdbl(dr("CURRENT_PREMIUM_AMOUNT"), 0)
                            ldr(0).Item("Column3" & xAction) += NZdbl(dr("RibsCount"), 0)
                            rtds.DailySummaryReportSummary.Rows(0)(xAction) += dr("ContactMethodCount")
                            rtds.DailySummaryReportSummary.Rows(0)(xAction & "Dollars") += dr("CURRENT_PREMIUM_AMOUNT")
                        End If
                    Catch ex As Exception
                        ldr(0).Item("DataError") += "Error: Closed Item last with Action (" & dr("xAction") & ")" & vbCrLf
                    End Try                    
                Next

                Return rtds
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Private Shared Sub AddUserToTable(ByRef dt As DataTable, ByVal UserID As String, ByVal dtContactMethods As DataTable)
            Try
                If dt.Rows.Count = 0 Then
                    For Each Action As DataRow In dtContactMethods.Rows
                        Dim strReportDisplayName As String = ReportContactMethodDisplayName(Action("ContactMethod").ToString).ToString
                        If strReportDisplayName <> "" Then
                            Dim newDR As DataRow = dt.NewRow
                            newDR("UserID") = UserID
                            newDR("ContactMethod") = strReportDisplayName
                            dt.Rows.Add(newDR)
                        End If
                    Next

                End If
                If dt.Select("UserID ='" & UserID & "'").Length = 0 Then
                    For Each Action As DataRow In dtContactMethods.Rows
                        Dim strReportDisplayName As String = ReportContactMethodDisplayName(Action("ContactMethod").ToString).ToString
                        If strReportDisplayName <> "" Then
                            Dim newDR As DataRow = dt.NewRow
                            newDR("UserID") = UserID
                            newDR("ContactMethod") = strReportDisplayName
                            dt.Rows.Add(newDR)
                        End If
                    Next
                End If

            Catch ex As Exception
                Throw ex
            End Try

        End Sub
    End Class

End Namespace



